﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.下单MTO;
using DXApplicationTangche.UC.抖音出入库.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.抖音出入库.抖音下单
{
    public partial class Form秒杀下单 : DevExpress.XtraEditors.XtraForm
    {
        //  下单接口返回
        private List<RetailUserOrderDTO> retailUserOrders = new List<RetailUserOrderDTO>();
        //  商品
        private RetailSkuDTO retailSkuDTO = new RetailSkuDTO();
        //  客户带地址
        private String mobile;
        private List<DouyinAddressQueryDTO> douyinAddressQueryDTOs;
        private DouyinAddressQueryDTO douyinAddressQuery = new DouyinAddressQueryDTO();
        public Form秒杀下单(RetailSkuDTO retailSkuDTO)
        {
            InitializeComponent();
            this.retailSkuDTO = retailSkuDTO;
            this.refresh客户();
            this.商品属性();
        }

        private void 商品属性()
        {
            this.textBox商品描述.Text = this.retailSkuDTO.description;
            this.textBox商品单价.Text = this.retailSkuDTO.price.ToString();
            this.textBox商品是否预售.Text = this.retailSkuDTO.ifPreSale?"是":"否";
            try
            {
                //显示图片
                this.pictureBox商品图片.LoadAsync(URL.图片url + URL.零售秒杀商品图片 + this.retailSkuDTO.pictureUrl);
            }
            catch { }
        }

        private void refresh客户()
        {
            this.douyinAddressQueryDTOs = 抖音Service.getDouyinUserAddressInfoAll("SSHIRT", "18");
            this.searchLookUpEdit客户.Properties.DataSource = this.douyinAddressQueryDTOs;
        }

        #region 选择客户
        private void searchLookUpEdit客户_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear客户Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit客户View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit客户.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    String mobile = this.searchLookUpEdit客户.Properties.View.GetRowCellValue(rowHandle, "mobile").ToString();
                    //  显示选择的客户
                    foreach (DouyinAddressQueryDTO item in this.douyinAddressQueryDTOs)
                    {
                        if (mobile.Equals(item.mobile))
                        {
                            this.douyinAddressQuery = item;
                            break;
                        }
                    }
                    this.textBox客户.Text = this.douyinAddressQuery.customerName;
                    this.textBox地址.Text = this.douyinAddressQuery.addressProvince + this.douyinAddressQuery.addressCity + this.douyinAddressQuery.addressDistrict + this.douyinAddressQuery.addressDetail;
                    this.textBox电话.Text = mobile;
                    //  查询客户历史订单
                    this.mobile = mobile;
                    this.查询客户历史订单();
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear客户Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit客户.ToolTip = null;
            searchLookUpEdit客户.EditValue = null;
        }
        private void searchLookUpEdit客户_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.douyinAddressQuery.customerName;
        }
        #endregion

        private void simpleButton新建客户_Click(object sender, EventArgs e)
        {
            new Form高德新建客户地址().ShowDialog();
            this.refresh客户();
        }

        private void simpleButton下单_Click(object sender, EventArgs e)
        {
            //  判断是否选择客户
            if (String.IsNullOrWhiteSpace(this.textBox电话.Text))
            {
                MessageBox.Show("请选择要下单客户");
                return;
            }
            //  判断尺码数量是否完整
            if (String.IsNullOrWhiteSpace(this.comboBox尺码.Text) || String.IsNullOrWhiteSpace(this.numericUpDown数量.Value.ToString()))
            {
                MessageBox.Show("请将尺码及数量填写完整");
                return;
            }
            //  下单
            RetailUserOrderDTO retailUserOrderDTO =
                零售Service.createRetailOrder(
                    this.retailSkuDTO.skuId, 
                    this.textBox电话.Text.Trim(), 
                    this.comboBox尺码.Text.Trim(), 
                    Convert.ToInt32(this.numericUpDown数量.Value)
                    );
            //  添加到已下单客户列表
            this.retailUserOrders.Add(retailUserOrderDTO);
            this.gridControl预下单.DataSource = this.retailUserOrders;
            this.gridView预下单.RefreshData();
            //  查询当前客户历史订单
            this.查询客户历史订单();
            //  清空
            this.textBox客户.Clear();
            this.textBox地址.Clear();
            this.textBox电话.Clear();
        }
        private void 查询客户历史订单()
        {
            List<RetailUserOrderDTO> retailUserOrderDTOs = 零售Service.getRetailUserOrderByMobile(this.mobile,"SSHIRT","18");
            this.gridControl历史订单.DataSource = retailUserOrderDTOs;
            this.gridView历史订单.RefreshData();
            this.gridView历史订单.ExpandAllGroups();
            try { this.label历史订单.Text = "当前选中客户:" + retailUserOrderDTOs[0].customerName + " " + "历史订单"; } catch { }
        }

        private void gridView历史订单_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            try
            {
                if (e.RowHandle >= 0)
                {
                    RetailUserOrderDTO retailUserOrderDTO = (RetailUserOrderDTO)gridView历史订单.GetRow(e.RowHandle);
                    if (!retailUserOrderDTO.ifDelivery)
                    {
                        if (e.Column.Name.Equals("gridColumn是否发货"))
                        {
                            e.Appearance.BackColor = Color.OrangeRed;
                            e.Appearance.ForeColor = Color.White;
                        }
                    }

                }
            }
            catch { }
        }

        private void gridView预下单_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            String mobile = this.gridView预下单.GetRowCellValue(this.gridView预下单.FocusedRowHandle, "mobile").ToString();
            this.mobile = mobile;
            this.查询客户历史订单();
        }

        private void 删除订单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //  仅未发货订单可以删除
            String id = this.gridView历史订单.GetRowCellValue(this.gridView历史订单.FocusedRowHandle, "id").ToString();
            String ifDelivery = this.gridView历史订单.GetRowCellValue(this.gridView历史订单.FocusedRowHandle, "ifDelivery").ToString();
            if (!Convert.ToBoolean(ifDelivery))
            {
                //  删除零售订单
                零售Service.deleteRetailOrderById(id);
                //  查询查询客户历史订单
                this.查询客户历史订单();
            }
            else
            {
                MessageBox.Show("仅未发货订单可以删除");
                return;
            }
        }
    }
}
